home *** CD-ROM | disk | FTP | other *** search
/ PC Format (South-Africa) 2001 June / PCFJune.iso / mweb / MWEB Utils / ws295sdk.exe / Ws2sdkzp.exe / SAMPLES / LAYERED / DOVERLAP.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-06-06  |  7.3 KB  |  310 lines

  1. /*++
  2.  
  3.      Copyright (c) 1996 Intel Corporation
  4.      Copyright (c) 1996 Microsoft Corporation
  5.      All Rights Reserved
  6.  
  7.      Permission is granted to use, copy and distribute this software and
  8.      its documentation for any purpose and without fee, provided, that
  9.      the above copyright notice and this statement appear in all copies.
  10.      Intel makes no representations about the suitability of this
  11.      software for any purpose.  This software is provided "AS IS."
  12.  
  13.      Intel specifically disclaims all warranties, express or implied,
  14.      and all liability, including consequential and other indirect
  15.      damages, for the use of this software, including liability for
  16.      infringement of any proprietary rights, and including the
  17.      warranties of merchantability and fitness for a particular purpose.
  18.      Intel does not assume any responsibility for any errors which may
  19.      appear in this software nor any responsibility to update it.
  20.  
  21.  
  22. Module Name:
  23.  
  24.     doverlap.cpp
  25.  
  26. Abstract:
  27.  
  28.     This module defines the layered class dprovider along with its methods.
  29.  
  30. --*/
  31.  
  32. #include "precomp.h"
  33.  
  34. DOVERLAPPEDSTRUCTMGR::DOVERLAPPEDSTRUCTMGR()
  35. /*++
  36.  
  37. Routine Description:
  38.  
  39.     DOVERLAPPEDSTRUCTMGR  object  constructor.   Creates and returns a
  40.     DOVERLAPPEDSTRUCTMGR object.  Note that  the  DOVERLAPPEDSTRUCTMGR object
  41.     has not been fully initialized.  The "Initialize" member function must be
  42.     the first member function called on the new DOVERLAPPEDSTRUCTMGR object.
  43.  
  44. Arguments:
  45.  
  46.     None
  47.  
  48. Return Value:
  49.  
  50.     None
  51. --*/
  52. {
  53.     // Setup the free list
  54.     m_overlapped_free_list.Next = NULL;
  55.     m_overlapped_struct_block = NULL;
  56.     InitializeCriticalSection(&m_overlapped_free_list_lock);
  57. }
  58.  
  59.  
  60. INT
  61. DOVERLAPPEDSTRUCTMGR::Initialize(
  62.         )
  63. /*++
  64.  
  65. Routine Description:
  66.  
  67.     The initialization routine for a DOVERLAPPEDSTRUCTMGR  object.  This
  68.     procedure completes the initialzation of the object.  This procedure
  69.     preforms initialization operations that may fail and must be reported since
  70.     there is no way to fail the constructor.
  71.  
  72. Arguments:
  73.  
  74.     None
  75. Return Value:
  76.  
  77.     The  function returns NO_ERROR if successful.  Otherwise it
  78.     returns an appropriate WinSock error code if the initialization
  79.     cannot be completed.
  80. --*/
  81. {
  82.     INT   ReturnCode;
  83.     ULONG BlockSize;
  84.     PBYTE CurrentBlock;
  85.     ULONG BytesAvailable;
  86.     ULONG StructSize;
  87.  
  88.  
  89.     ReturnCode = WSAENOBUFS;
  90.  
  91.     //
  92.     // Initialize the pool of internal overlapped structs.
  93.     //
  94.  
  95.     // Get memory for our overlapped structs
  96.     BlockSize = sizeof(INTERNALOVERLAPPEDSTRUCT) *
  97.         OUTSTANDINGOVERLAPPEDSTRUCTS;
  98.     m_overlapped_struct_block = (PBYTE) new BYTE[BlockSize];
  99.  
  100.     if (m_overlapped_struct_block){
  101.  
  102.         // Dice up the memory block into internal overlapped structs and add
  103.         // them to the free list.
  104.         EnterCriticalSection(&m_overlapped_free_list_lock);
  105.  
  106.         StructSize = sizeof(INTERNALOVERLAPPEDSTRUCT);
  107.  
  108.         BytesAvailable = BlockSize;
  109.         CurrentBlock   = m_overlapped_struct_block;
  110.  
  111.         while (BytesAvailable > StructSize){
  112.             PushEntryList(&m_overlapped_free_list,
  113.                           (PSINGLE_LIST_ENTRY) CurrentBlock);
  114.  
  115.             BytesAvailable -= StructSize;
  116.             CurrentBlock += StructSize;
  117.         } //while
  118.         ReturnCode = NO_ERROR;
  119.         LeaveCriticalSection(&m_overlapped_free_list_lock);
  120.     } //if
  121.  
  122.     return(ReturnCode);
  123. }
  124.  
  125.  
  126. DOVERLAPPEDSTRUCTMGR::~DOVERLAPPEDSTRUCTMGR()
  127. /*++
  128.  
  129. Routine Description:
  130.  
  131.     DOVERLAPPEDSTRUCTMGR object destructor.  This procedure has the
  132.     responsibility to perform any required shutdown operations for the
  133.     DOVERLAPPEDSTRUCTMGR object before the object memory is deallocated.
  134.  
  135. Arguments:
  136.  
  137.     None
  138.  
  139. Return Value:
  140.  
  141.     None
  142. --*/
  143. {
  144.     // Clean up the overlapped struct pool
  145.     EnterCriticalSection(&m_overlapped_free_list_lock);
  146.     delete(m_overlapped_struct_block);
  147.     m_overlapped_free_list.Next = NULL;
  148.     LeaveCriticalSection(&m_overlapped_free_list_lock);
  149.     DeleteCriticalSection(&m_overlapped_free_list_lock);
  150. }
  151.  
  152.  
  153. PINTERNALOVERLAPPEDSTRUCT
  154. DOVERLAPPEDSTRUCTMGR::AllocateOverlappedStruct(
  155.     )
  156. /*++
  157.  
  158. Routine Description:
  159.  
  160.     Allocates an interanl overlapped structure from the pool of available
  161.     structures.
  162. Arguments:
  163.  
  164.     None
  165.  
  166. Return Value:
  167.  
  168.     A pointer the a internal overlapped struct on success else NULL.
  169.  
  170. --*/
  171. {
  172.     PINTERNALOVERLAPPEDSTRUCT ReturnValue;
  173.  
  174.     ReturnValue = PopOverlappedStruct();
  175.  
  176.     if (ReturnValue){
  177.         ReturnValue->iolSignature = STRUCTSIGNATURE;
  178.     } //if
  179.  
  180.     return(ReturnValue);
  181. }
  182.  
  183.  
  184. VOID
  185. DOVERLAPPEDSTRUCTMGR::FreeOverlappedStruct(
  186.     LPWSAOVERLAPPED   OverlappedStruct
  187.     )
  188. /*++
  189.  
  190. Routine Description:
  191.  
  192.     Frees an interanl overlapped structure to the pool of available
  193.     structures.
  194. Arguments:
  195.  
  196.     OverlappedStruct - A pointer to the InternalOverlappedStruct member of an
  197.     interanl overlapped struct.
  198.  
  199. Return Value:
  200.  
  201.     NONE
  202.  
  203. --*/
  204. {
  205.     PushOverlappedStruct(OverlappedStruct);
  206. }
  207.  
  208.  
  209.  
  210. PINTERNALOVERLAPPEDSTRUCT
  211. DOVERLAPPEDSTRUCTMGR::PopOverlappedStruct()
  212. /*++
  213.  
  214. Routine Description:
  215.  
  216.     Pops a internal overlapped structure off of the free list and initializes
  217.     the structure.
  218.  
  219. Arguments:
  220.  
  221.     NONE
  222. Return Value:
  223.  
  224.     A pointer to a internal overlapped structure on success else NULL
  225.  
  226. --*/
  227. {
  228.     PINTERNALOVERLAPPEDSTRUCT ReturnValue;
  229.  
  230.     EnterCriticalSection(&m_overlapped_free_list_lock);
  231.  
  232.     ReturnValue =
  233.         (PINTERNALOVERLAPPEDSTRUCT)PopEntryList(&m_overlapped_free_list);
  234.  
  235.     LeaveCriticalSection(&m_overlapped_free_list_lock);
  236.  
  237.     if (ReturnValue){
  238.  
  239.         // Init the structure
  240.         ZeroMemory(
  241.             ReturnValue,
  242.             sizeof(INTERNALOVERLAPPEDSTRUCT));
  243.         ReturnValue->iolSignature = STRUCTSIGNATURE;
  244.     } //if
  245.  
  246.     return(ReturnValue);
  247. }
  248.  
  249.  
  250. VOID
  251. DOVERLAPPEDSTRUCTMGR::PushOverlappedStruct(
  252.     LPWSAOVERLAPPED OverlappedStruct
  253.     )
  254. /*++
  255.  
  256. Routine Description:
  257.  
  258.     Pushes an internal overlapped structure onto the free list.
  259.  
  260. Arguments:
  261.  
  262.     A pointer to the InteranlOverlappedStruct member of an internal overlapped
  263.     struct.
  264. Return Value:
  265.  
  266.     NONE
  267. --*/
  268. {
  269.     PINTERNALOVERLAPPEDSTRUCT InternalOverlappedStruct;
  270.  
  271.  
  272.     InternalOverlappedStruct = CONTAINING_RECORD (
  273.                             OverlappedStruct,
  274.                             INTERNALOVERLAPPEDSTRUCT,
  275.                             iolInternalOverlappedStruct);
  276.  
  277.     assert(STRUCTSIGNATURE == InternalOverlappedStruct->iolSignature);
  278.  
  279.     EnterCriticalSection(&m_overlapped_free_list_lock);
  280.  
  281.  
  282.     PushEntryList(&m_overlapped_free_list,
  283.                   (PSINGLE_LIST_ENTRY)InternalOverlappedStruct);
  284.  
  285.     LeaveCriticalSection(&m_overlapped_free_list_lock);
  286.  
  287. }
  288.  
  289.  
  290.  
  291.  
  292. PINTERNALOVERLAPPEDSTRUCT
  293. DOVERLAPPEDSTRUCTMGR::GetInternalOverlappedStructure(
  294.     IN  LPWSAOVERLAPPED                     pCompletedOverlappedStruct
  295.     )
  296. {
  297.     PINTERNALOVERLAPPEDSTRUCT pOverlappedStruct;
  298.  
  299.  
  300.     pOverlappedStruct = CONTAINING_RECORD (
  301.                             pCompletedOverlappedStruct,
  302.                             INTERNALOVERLAPPEDSTRUCT,
  303.                             iolInternalOverlappedStruct);
  304.  
  305.     if (STRUCTSIGNATURE == pOverlappedStruct->iolSignature)
  306.         return pOverlappedStruct;
  307.     else
  308.         return(NULL);
  309. }
  310.